/***************************************************************************
****************************************************************************
*   File: G:\Jackie\code\c working dir\FIRcoeff_1.c
*   Created by dsPIC FD Lite Version 1.0.0 Build 1 at 10:47:20 Jan 30 2012
*   C Code Generator - Version 4.0  
****************************************************************************
*  Code Fragment to implement filter
*
*  The functions defined in 'qed_filt.c' must be compiled and linked in.
*    This can be accomplished by either #include "qed_filt.c"
*    or by separately compiling and linking 'qed_filt.c'
*
*** following is actual code fragment
*  extern FIR_filter FIR_FIRcoeff_1;
*
*  init_fir_float (&FIR_FIRcoeff_1);  // initialize filter structure 
*
*  FIR_FIRcoeff_1.filter ( x, y, n, &FIR_FIRcoeff_1);  // x is an array of input samples
*                                            // y is an array of output samples
*                                            // n is number of samples to process
*                                            // &FIR_FIRcoeff_1 is a pointer to the
*                                            //    filter structure
*****************************************************************************
*  This is a complete program which can be compiled and run to test the filter.
*  To change this to a subroutine only, just add in this program or add globally
*     in "qed_cgen.h" the line with the definition of DEFINE_SUBROUTINE as follows
*  #define DEFINE_SUBROUTINE
*****************************************************************************
****************************************************************************/

/* qed_cgen.h contains definitions of filter structures and function prototypes */
#include "C:\Program Files (x86)\MDS\dsPICfdLite\h\qed_cgen.h"

/* filter functions are in files 'qed_filt.c'  */

float FIRcoeff_1_coeff[20] = {
   1.196289062500e-002F,  /* filter tap #    0 */
   1.922607421875e-002F,  /* filter tap #    1 */
   2.780151367188e-002F,  /* filter tap #    2 */
   3.735351562500e-002F,  /* filter tap #    3 */
   4.733276367188e-002F,  /* filter tap #    4 */
   5.709838867188e-002F,  /* filter tap #    5 */
   6.597900390625e-002F,  /* filter tap #    6 */
   7.333374023438e-002F,  /* filter tap #    7 */
   7.858276367188e-002F,  /* filter tap #    8 */
   8.132934570313e-002F,  /* filter tap #    9 */
   8.132934570313e-002F,  /* filter tap #   10 */
   7.858276367188e-002F,  /* filter tap #   11 */
   7.333374023438e-002F,  /* filter tap #   12 */
   6.597900390625e-002F,  /* filter tap #   13 */
   5.709838867188e-002F,  /* filter tap #   14 */
   4.733276367188e-002F,  /* filter tap #   15 */
   3.735351562500e-002F,  /* filter tap #   16 */
   2.780151367188e-002F,  /* filter tap #   17 */
   1.922607421875e-002F,  /* filter tap #   18 */
   1.196289062500e-002F}; /* filter tap #   19 */

float FIRcoeff_1_smpl[20];  /* sample delay line */ 

float FIRcoeff_1_gain = 1.000000000000e+000F; /* gain multiplier for FIR realization */

FIR_filter FIR_FIRcoeff_1 = {

     0,     /* quantization: 0 off, 1 on   */
     1,     /* quantization type */
            /*   0  Floating point         */
            /*   1  Fixed point fractional */
     0,     /* quantization type */
            /*   0 - No scaling of filter */
            /*   1 - Center tap scaled to be > 0.5 and < 1.0  */
            /*   2 - Center tap scaled to be 1.0 - gain factor
                     (essentially largest positive value) */
   0,           /* shift count - fixed point fractional */ 
   20,           /* filter length */ 
   0,            /* initial delay line index */
   &FIRcoeff_1_gain,  /* ptr to gain value          */
   FIRcoeff_1_coeff,  /* ptr to filter coefficients */
   FIRcoeff_1_smpl,   /* ptr to delay line */
   fir_float_reg}; /* ptr to filter routine */

/* call the following function first and normally only once */
/* init_fir_float (&FIR_FIRcoeff_1)  */
/*   where &FIR_FIRcoeff_1 is a pointer to the FIR_filter */
/*   structure defining the filter */


/* call the following function to filter n samples */
/* FIR_FIRcoeff_1.filter (pIn, pOut, int n, &FIR_FIRcoeff_1); */

/*   where pIn  is a pointer to an array or buffer of samples to be filtered */
/*         pOut is a pointer to the array of filtered samples output by the filter */
/*         n    is the number of samples to filter */
/*         &FIR_FIRcoeff_1 is a pointer to the structure defining the filter */


#ifndef DEFINE_SUBROUTINE

/* The following main program can be used to test the filter.         */
/*   input is in file 'in' and the filtered samples are in file 'out' */
/*   The input and output files are ascii floating point values       */
/*    e.g 1.0342 with 1 sample per line                               */
/* The input files can be created in DSPworks and exported as         */
/*   ascii floating point or any other system capable of creating     */
/*   ascii files with floating point values.                          */
/* The filtered output file can be imported into DSPworks as an ascii */
/*    floating point file and an FFT can be run to validate           */
/*    the frequency response.                                         */

#include "C:\Program Files (x86)\MDS\dsPICfdLite\src\qed_filt.c"
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <math.h>


#define INSZ1  1000
#define OUTSZ1 1000

static    float x[INSZ1], y[OUTSZ1];


int main(int argc, char *argv[])

{
  int i, in_count, file_status, error;

  FILE *fin;              /* input file of samples */
  FILE *fout;             /* output file of filtered samples */ 

  fprintf (stderr," ***** start filter test *****\n");

  fprintf (stderr," this program accepts 0,1 or 2 command line arguments\n");
  fprintf (stderr," the first  argument is the filename of the input file\n");
  fprintf (stderr," the second argument is the filename of the output file\n");
  fprintf (stderr," if there are 0 arguments, input and output is respectively\n");
  fprintf (stderr,"     stdin and stdout\n");
  fprintf (stderr," if only one argument is specified, then output is stdout\n");
  fprintf (stderr," if input is stdin rather than a file, then fscanf expects input\n");
  fprintf (stderr,"     from the console which may be piped in or entered directly\n");

  fin  = stdin;
  fout = stdout;
  error = 0;

  if (argc == 1) {
	    fprintf(stderr," ***** waiting for input *****\n");
  }
  if (argc >= 2) {
      fin = fopen (argv[1], "r");
      if (fin == NULL) {
          fprintf(stderr,"\n error - Cannot open file %s for input\n", argv[1]);
          error = 1;
      }
  }
  if (argc >= 3) {
      fout = fopen (argv[2], "w");
      if (fout == NULL) {
          fprintf(stderr,"\n error - Cannot open file %s for output\n", argv[2]);
          error = 1;
      }
  }
  if (error) {
      fprintf(stderr," ***** end filter test *****\n");
      return(0);
  }
  
  

  init_fir_float (&FIR_FIRcoeff_1);


  do {

      /* get input samples */ 
      for (in_count = 0; in_count < INSZ1; in_count++) { 
          file_status = fscanf(fin,"%f",&x[in_count]); 
          if (file_status != 1) 
              break; 
      }

      /* filter samples */ 

      if (in_count == 0) break;

      FIR_FIRcoeff_1.filter( x, y, in_count, &FIR_FIRcoeff_1);

      for (i = 0; i < in_count; i++)
          fprintf (fout,"%f\n",y[i]);

  } while (file_status == 1);

  fclose (fin); 
  fclose (fout);

  fprintf(stderr," ***** end filter test *****\n");
  return(1);

}
 
#endif 
